const jsonwebtoken = require('jsonwebtoken')
const User = require('../models/users')
const Question = require('../models/questions');
const Answer = require('../models/answers');//引用答案模型

const { secret } = require('../config');

class UsersCtl {
    //获取所有数据
    async find(ctx){
        const { per_page = 3 } = ctx.query;
        const page = Math.max(ctx.query.page * 1, 1) - 1;
        const parPage = Math.max(per_page * 1, 1);
        ctx.body = await User.find({ name: new RegExp(ctx.query.q) })
        .limit(parPage).skip(page * parPage);
    }
    //获取指定数据
    async findById(ctx){
        const { fields = '' } = ctx.query; //默认为空
        const selectFields = fields.split(';').filter(f => f).map(f => ' +' + f).join('');//转成数组、是否存在、拼接、转字符串
        const populateStr = fields.split(';').filter(f => f).map(f => {
            if (f === 'employments') {
              return 'employments.company employments.job';
            }
            if (f === 'educations') {
              return 'educations.school educations.major';
            }
            return f;
          }).join(' ');
        const user = await User.findById(ctx.params.id).select(selectFields)//查询需要的字段
        .populate(populateStr);//加populate可以获取用户信息;
        if(!user) { ctx.throw(404, '用户不存在');}
        ctx.body = user;
    }
    //创建数据
    async create(ctx){
        ctx.verifyParams({
            name:{ type:'string', required: true},
            password: { type:'string', required: true }
        });
        const { name } = ctx.request.body;
        const repeatedUser = await User.findOne({ name });
        if(repeatedUser) { ctx.throw(409, '用户已经占用'); }
        const user = await new User(ctx.request.body).save();
        ctx.body = user;
    }
    //验证权限
    async checkOwner(ctx, next){
        if(ctx.params.id !== ctx.state.user._id){ ctx.throw(403, '没有权限'); }
        await next();
    }
    //更新数据
    async update(ctx){
        ctx.verifyParams({
            name:{ type:'string', required: false},
            password: { type:'string', required: false },
            avatar_url: {type:'string',required: false},
            gender: {type:'string',required: false},//性别
            headline: {type:'string',required: false},//个性签名
            locations: {type:'array',itemType: 'string',required: false},
            business: {type:'string',required: false},//行业
            employments: {type:'array',itemType: 'object',required: false},//职业经历
            educations: {type:'array',itemType: 'object',required: false}//教育经历
        })
        const user = await User.findByIdAndUpdate(ctx.params.id, ctx.request.body);
        if(!user) { ctx.throw(404, '用户不存在');}
        ctx.body = user;
    }
    //删除数据
    async delete(ctx){
        const user = await User.findByIdAndRemove(ctx.params.id);
        if(!user) { ctx.throw(404, '用户不存在');}
        ctx.status = 204;
    }
    //用户登录
    async login(ctx){
        ctx.verifyParams({
            name:{ type:'string', required: true},
            password: { type:'string', required: true }
        })
        const user = await User.findOne(ctx.request.body);
        if(!user) { ctx.throw(401, '用户名或密码不正确');}
        const {_id, name} = user;
        const token = jsonwebtoken.sign({_id, name}, secret, { expiresIn: '1d' });
        ctx.body = { token };
    }
    //获取某个用户的关注人列表
    async listFollowing(ctx){
        const user = await User.findById(ctx.params.id).select('+following').populate('following');//加populate可以获取用户信息
        if (!user) { ctx.throw(404, '用户不存在'); }
        ctx.body = user.following;
    }
    //获取某个用户的粉丝列表
    async listFollowers (ctx){
        const users = await User.find({following: ctx.params.id});//查询所有用户符合id的列表
        ctx.body = users;
    }
    //检查用户是否存在
    async checkUserExist(ctx, next){
        const user = await User.findById(ctx.params.id);
        if(!user){ ctx.throw(404, '用户不存在'); }
        await next();
    }
    //关注某人
    async follow(ctx){
        const me = await User.findById(ctx.state.user._id).select('+following');
        if(!me.following.map(id => id.toString()).includes(ctx.params.id)){
            me.following.push(ctx.params.id);
            me.save();
        }
        ctx.status = 204;
    }
    //取消关注
    async unfollow(ctx){
        const me = await User.findById(ctx.state.user._id).select('+following');
        const index = me.following.map(id => id.toString()).indexOf(ctx.params.id);
        if(index > -1){
            me.following.splice(index, 1);
            me.save();
        }
        ctx.status = 204;
    }

    //获取某个用户关注的话题列表
    async listFollowingTopics(ctx) {
        const user = await User.findById(ctx.params.id).select('+followingTopics').populate('followingTopics');
        if (!user) { ctx.throw(404, '用户不存在'); }
        ctx.body = user.followingTopics;
    }
    //关注话题
    async followTopic(ctx){
        const me = await User.findById(ctx.state.user._id).select('+followingTopics');
        if(!me.followingTopics.map(id => id.toString()).includes(ctx.params.id)){
            me.followingTopics.push(ctx.params.id);
            me.save();
        }
        ctx.status = 204;
    }
    //取消关注话题
    async unfollowTopic(ctx){
        const me = await User.findById(ctx.state.user._id).select('+followingTopics');
        const index = me.followingTopics.map(id => id.toString()).indexOf(ctx.params.id);
        if(index > -1){
            me.followingTopics.splice(index, 1);
            me.save();
        }
        ctx.status = 204;
    }
    //获取某个问题的提出者
    async listQuestions(ctx) {
        const questions = await Question.find({ questioner: ctx.params.id });
        ctx.body = questions;
    }
    //列出喜欢的答案
    async listLikingAnswers(ctx) {
        const user = await User.findById(ctx.params.id).select('+likingAnswers').populate('likingAnswers');
        if (!user) { ctx.throw(404, '用户不存在'); }
        ctx.body = user.likingAnswers;
    }
    //喜欢的答案
    async likeAnswer(ctx, next) {
        const me = await User.findById(ctx.state.user._id).select('+likingAnswers');
        if (!me.likingAnswers.map(id => id.toString()).includes(ctx.params.id)) {
          me.likingAnswers.push(ctx.params.id);
          me.save();
          await Answer.findByIdAndUpdate(ctx.params.id, { $inc: { voteCount: 1 } });
        }
        ctx.status = 204;
        await next();
    }
    //取消某个喜欢答案
    async unlikeAnswer(ctx) {
        const me = await User.findById(ctx.state.user._id).select('+likingAnswers');
        const index = me.likingAnswers.map(id => id.toString()).indexOf(ctx.params.id);
        if (index > -1) {
          me.likingAnswers.splice(index, 1);
          me.save();
          await Answer.findByIdAndUpdate(ctx.params.id, { $inc: { voteCount: -1 } });
        }
        ctx.status = 204;
    }
    //列出踩过的答案
    async listDislikingAnswers(ctx) {
        const user = await User.findById(ctx.params.id).select('+dislikingAnswers').populate('dislikingAnswers');
        if (!user) { ctx.throw(404, '用户不存在'); }
        ctx.body = user.dislikingAnswers;
    }
    //踩过的答案
    async dislikeAnswer(ctx, next) {
        const me = await User.findById(ctx.state.user._id).select('+dislikingAnswers');
        if (!me.dislikingAnswers.map(id => id.toString()).includes(ctx.params.id)) {
          me.dislikingAnswers.push(ctx.params.id);
          me.save();
        }
        ctx.status = 204;
        await next();
    }
    //取消某个踩过的答案
    async undislikeAnswer(ctx) {
        const me = await User.findById(ctx.state.user._id).select('+dislikingAnswers');
        const index = me.dislikingAnswers.map(id => id.toString()).indexOf(ctx.params.id);
        if (index > -1) {
          me.dislikingAnswers.splice(index, 1);
          me.save();
        }
        ctx.status = 204;
    }
    //列出收藏的答案
    async listCollectingAnswers(ctx) {
        const user = await User.findById(ctx.params.id).select('+collectingAnswers').populate('collectingAnswers');
        if (!user) { ctx.throw(404, '用户不存在'); }
        ctx.body = user.collectingAnswers;
    }
    //收藏答案
    async collectAnswer(ctx, next) {
        const me = await User.findById(ctx.state.user._id).select('+collectingAnswers');
        if (!me.collectingAnswers.map(id => id.toString()).includes(ctx.params.id)) {
          me.collectingAnswers.push(ctx.params.id);
          me.save();
        }
        ctx.status = 204;
        await next();
    }
    //取消收藏答案
    async uncollectAnswer(ctx) {
        const me = await User.findById(ctx.state.user._id).select('+collectingAnswers');
        const index = me.collectingAnswers.map(id => id.toString()).indexOf(ctx.params.id);
        if (index > -1) {
          me.collectingAnswers.splice(index, 1);
          me.save();
        }
        ctx.status = 204;
    }
}

module.exports = new UsersCtl();